import streamlit as st
import time
import numpy as np

st.set_page_config(page_title="Plotting Demo", page_icon="📈")

st.markdown("# Plotting Demo")
st.sidebar.header("Plotting Demo")
st.write(
    """This demo illustrates a combination of plotting and animation with
Streamlit. We're generating a bunch of random numbers in a loop for around
5 seconds. Enjoy!"""
)

# 初始化进度条和状态文本
progress_bar = st.sidebar.progress(0)
status_text = st.sidebar.empty()

# 生成初始数据
last_rows = np.random.randn(1, 1)

# 使用初始数据创建折线图
chart = st.line_chart(last_rows)

# 循环以模拟数据更新
for i in range(1, 101):
    # 生成新数据行，模拟数据流动
    new_rows = last_rows[-1, :] + np.random.randn(5, 1).cumsum(axis=0)
    
    # 更新状态文本，显示完成百分比
    status_text.text("%i%% Complete" % i)
    
    # 向折线图添加新数据行
    chart.add_rows(new_rows)
    
    # 更新进度条
    progress_bar.progress(i)
    
    # 用新数据行更新last_rows，为下一次循环做准备
    last_rows = new_rows
    
    # 模拟数据处理时间
    time.sleep(0.05)

# 清空进度条，表示数据更新完成
progress_bar.empty()
# Streamlit widgets automatically run the script from top to bottom. Since
# this button is not connected to any other logic, it just causes a plain
# rerun.
st.button("Re-run")